<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>户外光案例</title>
    <style type="text/css">
        html, body {
            margin: 0;
            height: 100%;
        }

        canvas {
            display: block;
        }

    </style>
</head>
<body onload="draw();">

</body>
<script src="../js/three.js"></script>
<script src="../js/stats.min.js"></script>
<script src="../js/dat.gui.min.js"></script>
<script>
    var renderer, camera, scene, gui, stats, hemisphereLight;
    var sphere, cube, plane;

    function initRender() {
        renderer = new THREE.WebGLRenderer({antialias: true});
        renderer.setSize(window.innerWidth, window.innerHeight);
        //告诉渲染器需要阴影效果
        renderer.shadowMap.enabled = true;
        renderer.shadowMap.type = THREE.PCFSoftShadowMap; // 默认的是，没有设置的这个清晰 THREE.PCFShadowMap
        document.body.appendChild(renderer.domElement);
    }

    function initCamera() {
        camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.set(0, 100, 200);
        camera.lookAt(new THREE.Vector3(0, 0, 0));
    }

    function initScene() {
        scene = new THREE.Scene();
    }

    function initGui() {
        //声明一个保存需求修改的相关数据的对象
        gui = {
            skyColor: "#00ffff", //环境光源
            groundColor: "#ff0000", //点光源
            intensity: 1, //灯光强度
            visible: true, //显示户外光
        };

        var datGui = new dat.GUI();
        //将设置属性添加到gui当中，gui.add(对象，属性，最小值，最大值）
        datGui.addColor(gui, "skyColor").name("天空颜色").onChange(function (e) {
            hemisphereLight.color.set(e);
        });
        datGui.addColor(gui, "groundColor").name("地面颜色").onChange(function (e) {
            hemisphereLight.groundColor.set(e);
        });
        datGui.add(gui, "intensity", 0, 5).name("光线强度").onChange(function (e) {
            hemisphereLight.intensity = e;
        });
        datGui.add(gui, "visible").name("显示户外光").onChange(function (e) {
            hemisphereLight.visible = e;
        });
    }

    function initLight() {
        var ambientLight = new THREE.AmbientLight("#111111");
        scene.add(ambientLight);

        //添加户外光源
        hemisphereLight = new THREE.HemisphereLight(0x00ffff, 0xff0000, 1);
        scene.add(hemisphereLight);
    }

    function initModel() {

        //球体
        var sphereGeometry = new THREE.SphereGeometry(15, 24, 16);
        var sphereMaterial = new THREE.MeshPhongMaterial({color: 0xffffff});

        sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
        sphere.position.set(0, 50, 0);

        sphere.castShadow = true; //开启阴影

        scene.add(sphere);

        //立方体
        var cubeGeometry = new THREE.CubeGeometry(10, 10, 10);

        var cubeMaterial = new THREE.MeshLambertMaterial({color: 0x00ffff});

        cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
        cube.position.x = 30;
        cube.position.z = -5;

        cube.castShadow = true; //开启阴影

        scene.add(cube);

        //底部平面
        var planeGeometry = new THREE.PlaneGeometry(100, 100);
        var planeMaterial = new THREE.MeshLambertMaterial({color: 0xaaaaaa});

        plane = new THREE.Mesh(planeGeometry, planeMaterial);
        plane.rotation.x = -0.5 * Math.PI;
        plane.position.y = -5;

        plane.receiveShadow = true; //可以接收阴影

        scene.add(plane);

    }

    function initStats() {
        stats = new Stats();
        document.body.appendChild(stats.dom);
    }

    function render() {
        renderer.render(scene, camera);
    }

    //窗口变动触发的函数
    function onWindowResize() {

        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();
        renderer.setSize(window.innerWidth, window.innerHeight);

    }

    function animate() {
        //更新控制器
        render();

        //更新性能插件
        stats.update();

        requestAnimationFrame(animate);
    }

    function draw() {
        initGui();
        initRender();
        initScene();
        initCamera();
        initLight();
        initModel();
        initStats();

        animate();
        window.onresize = onWindowResize;
    }
</script>
</html>